package com.example.restfull.controller;

import com.example.restfull.bean.ResponseInfo;
import com.example.restfull.bean.UserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/***
 Created by grantwoo at 10/18/18 9:58 PM

 */
@RestController
@RequestMapping(value="/api/user")
public class UserController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    /**创建用户数据
     * @param errors 与@Valid 注解一起使用
     * */
    @PostMapping
    public UserInfo createUser(@Valid  @RequestBody UserInfo userInfo , BindingResult errors){

        userInfo.setId(123);

        return userInfo;
    }

    @PutMapping
    public ResponseInfo updateUser(@Valid  @RequestBody UserInfo userInfo, BindingResult errors){
        ResponseInfo responseInfo = new ResponseInfo();
        responseInfo.setCode(201);
        responseInfo.setMsg("update user success");

        return responseInfo;
    }

    @DeleteMapping(value="/{id}")
    public ResponseInfo deleteUser(@PathVariable String id){

        ResponseInfo responseInfo = new ResponseInfo();
        responseInfo.setCode(202);
        responseInfo.setMsg("delete user success");

        return responseInfo;
    }


    /**模糊查询相关用户数据*/
    @GetMapping
    public List<UserInfo> queryLike(@RequestParam int age, int sex){

        List<UserInfo> list = new ArrayList<>();
        list.add(new UserInfo());
        list.add(new UserInfo());
        list.add(new UserInfo());


        return list;
    }

    @GetMapping(value="/{id}")
    public UserInfo query(@PathVariable String id){

        logger.info("Query UserInfo by id:{}",id);

        UserInfo userInfo = new UserInfo();
        userInfo.setUsername("Tom");


        return userInfo;
    }

}
